package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.service.PurchaseListService;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author WZhaokang
 * @date 2020/12/29 12:38
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {

    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;
    @Autowired
    private PurchaseListDao purchaseListDao;
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;


    /**
     * 根据进货单id查询进货单商品信息
     * @param purchaseListId
     * @return
     */
    @Override
    public List<PurchaseListGoods> goodsList(Integer purchaseListId) {
        List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.getList(purchaseListId);
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            System.out.println(purchaseListGoods.getGoodsName());
            System.out.println(purchaseListGoods.getGoodsCode());
        }
        return purchaseListGoodsList;
    }

    //根据进货单id删除进货单
    @Override
    @Transactional
    public void delete(Integer purchaseListId) {
        purchaseListGoodsDao.deleteByPurchaseListId(purchaseListId);
        purchaseListDao.deleteByPurchaseListId(purchaseListId);
    }

    @Override
    public List<PurchaseVO> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        ArrayList arrayList = new ArrayList();
        //四个表关联查询
        //t_purchase_list---> number:JH1605773419636   date:"2020-11-19"
        //通过时间区间查询 number
        List<PurchaseVO> purchaseLists = purchaseListDao.findList(sTime, eTime, goodsTypeId, codeOrName);

//        List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.selectByCodeOrName(codeOrName);
        //t_supplier---->supplier_name:四川耀荣汇商贸有限公司
        for (PurchaseVO purchaseList : purchaseLists) {
            Map map = new HashMap();
            /*Integer supplierId = purchaseList.getSupplierId();
            List<PurchaseListGoods> list = purchaseListGoodsDao.getList(purchaseList.getPurchaseListId());
            for (PurchaseListGoods purchaseListGoods : list) {
                map.put("number", purchaseList.getPurchaseNumber());
                map.put("date", purchaseList.getPurchaseDate());
                map.put("supplierName", purchaseList.getSupplierName());
                map.put("code", purchaseListGoods.getGoodsCode());
                map.put("name", purchaseListGoods.getGoodsName());
                map.put("model", purchaseListGoods.getGoodsModel());
                GoodsType goodsType = goodsTypeDao.getGoodsTypeById(purchaseListGoods.getGoodsTypeId());
                map.put("goodsType", goodsType.getGoodsTypeName());
                map.put("unit", purchaseListGoods.getGoodsUnit());
                map.put("price", purchaseListGoods.getPrice());
                map.put("num", purchaseListGoods.getGoodsNum());
                map.put("total", purchaseListGoods.getTotal());
            }
            arrayList.add(map);*/
            map.put("number", purchaseList.getNumber());
            map.put("date", purchaseList.getDate());
            map.put("supplierName", purchaseList.getSupplierName());
            map.put("code", purchaseList.getCode());
            map.put("name", purchaseList.getName());
            map.put("model", purchaseList.getModel());
            map.put("goodsType", purchaseList.getGoodsType());
            map.put("unit", purchaseList.getUnit());
            map.put("price", purchaseList.getPrice());
            map.put("num", purchaseList.getNum());
            map.put("total", purchaseList.getTotal());

            arrayList.add(map);
        }

        //t_purchase_list_goods --->
        //t_goods_type -->goods_type_name2:休闲食品
        return arrayList;
    }

   /* @Override
    public List<Map> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map> arrayList = new ArrayList();
        List<PurchaseList> purchaseLists = purchaseListDao.list(null,null,null,sTime,eTime);
        for (PurchaseList purchaseList : purchaseLists) {
            Map m = new HashMap();
            Integer purchaseListId = purchaseList.getPurchaseListId();
            List<PurchaseListGoods> list = purchaseListGoodsDao.getList(purchaseListId);
            if (goodsTypeId != null){
                //联合查询
                List<Map> purchaseListAndGoodsType = purchaseListGoodsDao.selectByGoodsTypeId(goodsTypeId);
                for (Map map : purchaseListAndGoodsType) {
                    map.get("code");
                    map.get("name");
                    map.get("model");
                    map.get("goodsType");
                    map.get("unit");
                    map.get("num");
                    map.get("total");
                }
                if ()

            }

        }
        return null;
    }*/
}
